package com.ruoyi.project.digital.service.impl;

import java.util.Date;
import java.util.List;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.DateRangeExample;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.project.digital.domain.Constants;
import com.ruoyi.project.digital.domain.TBack;
import com.ruoyi.project.digital.domain.UserBack;
import com.ruoyi.project.digital.domain.enums.YesOrNoStatus;
import com.ruoyi.project.digital.domain.vo.PetMessageVO;
import com.ruoyi.project.digital.service.ITBackService;
import com.ruoyi.project.digital.service.IUserBackService;
import com.ruoyi.project.system.service.ISysConfigService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.digital.mapper.PetMessageMapper;
import com.ruoyi.project.digital.domain.PetMessage;
import com.ruoyi.project.digital.service.IPetMessageService;

/**
 * 用户宠物聊天信息Service业务层处理
 *
 * @author ruoyi
 * @date 2023-11-01
 */
@Service
public class PetMessageServiceImpl extends ServiceImpl<PetMessageMapper, PetMessage> implements IPetMessageService
{

    @Autowired
    private IUserBackService userBackService;

    @Autowired
    private ITBackService backService;
    @Autowired
    private ISysConfigService sysConfigService;

    /**
     * 查询用户宠物聊天信息
     *
     * @param id 用户宠物聊天信息ID
     * @return 用户宠物聊天信息
     */
    @Override
    public PetMessage selectPetMessageById(Long id)
    {
        return baseMapper.selectPetMessageById(id);
    }

    /**
     * 查询用户宠物聊天信息列表
     *
     * @param petMessage 用户宠物聊天信息
     * @return 用户宠物聊天信息
     */
    @Override
    public List<PetMessage> selectPetMessageList(PetMessage petMessage)
    {
        return baseMapper.selectPetMessageList(petMessage);
    }

    /**
     * 新增用户宠物聊天信息
     *
     * @param petMessage 用户宠物聊天信息
     * @return 结果
     */
    @Override
    public int insertPetMessage(PetMessage petMessage)
    {
        return baseMapper.insertPetMessage(petMessage);
    }

    /**
     * 修改用户宠物聊天信息
     *
     * @param petMessage 用户宠物聊天信息
     * @return 结果
     */
    @Override
    public int updatePetMessage(PetMessage petMessage)
    {
        return baseMapper.updatePetMessage(petMessage);
    }

    /**
     * 批量删除用户宠物聊天信息
     *
     * @param ids 需要删除的用户宠物聊天信息ID
     * @return 结果
     */
    @Override
    public int deletePetMessageByIds(Long[] ids)
    {
        return baseMapper.deletePetMessageByIds(ids);
    }

    /**
     * 删除用户宠物聊天信息信息
     *
     * @param id 用户宠物聊天信息ID
     * @return 结果
     */
    @Override
    public int deletePetMessageById(Long id)
    {
        return baseMapper.deletePetMessageById(id);
    }

    @Override
    public Long creatMessge(Long id, Long petId) {
        QueryWrapper<UserBack> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("user_id",id);
        queryWrapper.eq("is_deafult", YesOrNoStatus.YES.getValue());
        UserBack userBack=userBackService.getOne(queryWrapper);
        if(ObjectUtil.isNull(userBack)){
            userBack=new UserBack();
            String key=   sysConfigService.selectConfigByKey(Constants.BACK_URL);
             TBack back=backService.getById(key);
            userBack.setBackUrl(back.getUrl());
            userBack.setBackId(back.getId());
            userBack.setUserId(id);
            userBackService.save(userBack);
        }
        TBack tBack=backService.getById(userBack.getBackId());
        PetMessage  message=new PetMessage();
        message.setNumber(0L);
        message.setDuration("0");
        message.setPetId(petId);
        message.setUserId(id);
        message.setStartTime(new Date());
        message.setBackId(tBack.getId());
        message.setBackUrl(tBack.getUrl());
        save(message);

      return  message.getId();
    }

    @Override
    public List<PetMessageVO> messageList(Long userId,String keywords,String times) {
        QueryWrapper<PetMessage> q = new QueryWrapper<>();
        q.eq("user_id",userId);
        q.like(StringUtils.isNotEmpty(keywords),"title", keywords);
        Date date = new Date();
        switch (times){
            case "一周内":
                q.between("start_time",DateRangeExample.getStartOfWeek(date),DateRangeExample.getEndOfWeek(date));
            case  "一月内":
                q.between("start_time",DateRangeExample.getStartOfMonth(date),DateRangeExample.getEndOfMonth(date));
            case  "半年内":
                q.between("start_time",DateRangeExample.getStartOfHalfYear(date),DateRangeExample.getEndOfHalfYear(date));
            case  "一年内":
                q.between("start_time",DateRangeExample.getStartOfYear(date),DateRangeExample.getEndOfYear(date));
        }
        return baseMapper.selectFront(q);
    }
}
